<?php

namespace app\index\controller;

use app\index\controller\User;
use think\facade\Config;
use think\facade\Session;

/**
 * 流量情况
 */
class Flow extends User
{    
    public $token = '';
    public function initialize()
    {
        parent::initialize();
        $this->token = session("login_token");       
        if(!$this->token){
            $this->redirect("index/user/login");
        }
    }
    
    /**
     * 流量情况（展示页面）
     */
    public function info(){
        $this->view->assign('title', __('流量情况')); 
        return $this->view->fetch();
    }

    /**
     * 流量分布
     */
    public function flow(){        
        $type = input('type');
        $res = $this->curl($type);
        $data = array();
        // $res['code'] = 401;
        switch ($res['code']) {
            case '1':
                foreach($res['data']['names'] as $k=>&$v){
                    $data[$k]['name'] = $v;
                    $data[$k]['data'] = isset($res['data']['datas'][$k])?$res['data']['datas'][$k]:0;
                    $data[$k]['color'] = isset($res['data']['colors'][$k])?$res['data']['colors'][$k]:'#000000';
                }
            break;
            case '401':
                $data = [];
                break;
        }        
        
        $arr['code'] = $res['code'];
        $arr['msg'] = $res['msg'];
        $arr['data'] = $data;
        die(json_encode($arr,JSON_UNESCAPED_UNICODE));
    }

    /**
     * 连接分布
     */
    public function link(){     
        $type = input('type');
        $res = $this->curl($type);
        $data = array();
        // $res['code'] = 401;       
        switch ($res['code']) {
            case '1':
                foreach($res['data']['names'] as $k=>&$v){
                    $data[$k]['name'] = $v;
                    $data[$k]['data'] = isset($res['data']['datas'][$k])?$res['data']['datas'][$k]:0;
                    $data[$k]['color'] = isset($res['data']['colors'][$k])?$res['data']['colors'][$k]:'#000000';
                }
            break;
            case '401':
                $data = [];
                break;
        }        
        $arr['code'] = $res['code'];
        $arr['msg'] = $res['msg'];
        $arr['data'] = $data;    
        die(json_encode($arr,JSON_UNESCAPED_UNICODE));
    }

    /**
     * snap快照
     */
    public function snap(){        
        $type = input('type');
        $res = $this->curl($type);
        // dump($res);die;   
        die(json_encode($res['data'],JSON_UNESCAPED_UNICODE));
    }


    /**
     * 请求接口 
     *
     * @param      <type>  $type   The type
     *
     * @return     <type>  ( description_of_the_return_value )
     */
    public function curl($type){
        $data = array(
            'type' => $type,
            'token' => $this->token,
            'terminal' => 'web'
        );
        $data_string = json_encode($data,JSON_UNESCAPED_UNICODE);
        $curlopt = array(
            CURLOPT_HTTPHEADER=>array ('Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen ( $data_string ) ),
            CURLOPT_TIMEOUT=>Config::get("site.api_timeout"),
        );
        $url = request()->domain().'/api/flow/data';
        // echo $url;die;
        // dump($curlopt);die;
        // dump($data_string);die;
        $res = curl_post($url,$data_string,$curlopt);
        $res = json_decode($res,true);
        return $res;
    }

}
